package com.example.springcloudgateway2demo.config;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * @author Liunh
 */
@Slf4j
@Component
public class AuthorizeGatewayFilterFactory extends AbstractGatewayFilterFactory<AuthorizeGatewayFilterFactory.Config> {

    /**
     * 不设防
     */
    private static final String AUTH_NOT = "not";
    /**
     * 静态资源文件
     */
    private static final String AUTH_STATIC = "static";
    /**
     * 网页端
     */
    private static final String AUTH_WEB = "web";
    /**
     * 中台
     */
    private static final String AUTH_BACK = "back";
    /**
     * 埋点
     */
    private static final String AUTH_DOT = "dot";
    /**
     * 客户端接口
     */
    private static final String AUTH_API = "api";
    /**
     * api接口加密
     */
    private static final String AUTH_API_EN = "apien";
    /**
     * API未登录
     */
    private static final String AUTH_API_NOT = "apinot";

    public AuthorizeGatewayFilterFactory() {
        //固定写法
        super(Config.class);
        log.info("Loaded GatewayFilterFactory [Authorize]");
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("enabled");
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            if (config.getType().equals(AUTH_API)) {
                //api已登录
                DataBuffer buffer = exchange.getResponse().bufferFactory().wrap("{\"code\":10011,\"msg\":\"权限校验不通过\"}".getBytes(StandardCharsets.UTF_8));
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                exchange.getResponse().getHeaders().add("Content-Type", "application/json;charset=UTF-8");
                return exchange.getResponse().writeWith(Mono.just(buffer));
            }

            if (config.getType().equals(AUTH_NOT)) {
                //不设防
                return chain.filter(exchange);
            }
            if (config.getType().equals(AUTH_API_EN)) {
                //API接口加密
                return chain.filter(exchange);
            }
            if (config.getType().equals(AUTH_API_NOT)) {
                //API未登录
                return chain.filter(exchange);
            }
            if (config.getType().equals(AUTH_STATIC)) {
                //静态资源文件
                return chain.filter(exchange);
            }
            if (config.getType().equals(AUTH_WEB)) {
                //网页端
                return chain.filter(exchange);
            }
            if (config.getType().equals(AUTH_BACK)) {
                //中台
                return chain.filter(exchange);
            }

            if (config.getType().equals(AUTH_DOT)) {
                //埋点
                return chain.filter(exchange);
            }

            DataBuffer buffer = exchange.getResponse().bufferFactory().wrap("{\"code\":10086,\"msg\":\"请配置权限\"}".getBytes(StandardCharsets.UTF_8));
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            exchange.getResponse().getHeaders().add("Content-Type", "application/json;charset=UTF-8");
            return exchange.getResponse().writeWith(Mono.just(buffer));
        };
    }

    @Getter
    @Setter
    public static class Config {
        /**
         * 过滤类型：not-不设防（正常玩野），web-网页端，api-前端（），api-not-前端未登录，static-静态/资源文件（css样式表等），back-中台，dot-埋点
         */
        private String type;

    }

}
